﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SiteAgle.Models;
using SiteAgle.ViewHelper;
using System.Data;
using System.Data.SqlClient;

namespace SiteAgle.Controllers
{
    public class TarefaController : GeneralController
    {
        //
        // GET: /Tarefa/

        public ActionResult Index(int id)
        {
            ViewData["SprintID"] = id;
            return View(DaoTarefa.ListarPorSprint(id));
        }

        //
        // GET: /Tarefa/Details/5

        public ActionResult Detalhe(int id)
        {
            return View();
        }

        //
        // GET: /Tarefa/Create

        public ActionResult Novo(int id)
        {
            Tarefa taf = new Tarefa();
            taf.IdSprint = id;

            return View(taf);
            //return View(new Tarefa()
            //{
            //    IdSprint = id,
            //    Sprint = new DaoSprint().ObterPorID(id)
            //});
        }

        //
        // POST: /Tarefa/Create

        [HttpPost]
        public ActionResult Novo(Tarefa _tarefa, FormCollection collection)
        {
            //try
            //{
            _tarefa.IdStatus = (int)EnumStatusTarefa.NAOINICIADO;
            _tarefa.StatusTarefas = DaoStatusTarefa.ObterPorID((int)EnumStatusTarefa.NAOINICIADO);
            _tarefa.Criacao = DateTime.Now;
                        //_tarefa.Sprint = new DaoSprint().ObterPorID(_tarefa.IdSprint);
            if (_tarefa.IdUsuario == 0)
            {
                _tarefa.IdUsuario = null;
            }
            new DaoTarefa().Salvar(_tarefa);
                // TODO: Add insert logic here
                //  return View("Index",tarefa.IdSprint);
            Session["sucesso"] = "Criado com sucesso";
            return Redirect("~/tarefa/index/" + _tarefa.IdSprint);
                //  return RedirectToAction("Index","Tarefa",tarefa.IdSprint);
            //}
            //catch (UpdateException ex)
            //{
            //    SqlException innerException = ex.InnerException as SqlException;
            //    if (innerException != null && innerException.Message.StartsWith("Violation of UNIQUE KEY constraint 'Unique_GiftId'"))
            //    {
            //        // handle exception here..
            //    }
            //    else
            //    {
            //        throw;
            //    }

            //}

            return View();
        }
        [HttpPost]
        public String AlterarStatus(int idTarefa, int status, int idUser)
        {
            Usuario user = new DaoUsuario().ObterPorID(idUser);
            StatusTarefas statusTarefa = DaoStatusTarefa.ObterPorID(status);
            DaoTarefa daotarefa = new DaoTarefa();
            Tarefa tarefa = daotarefa.ObterPorID(idTarefa);
            tarefa.IdUsuario = idUser;
            tarefa.Usuario = user;
            tarefa.StatusTarefas = statusTarefa;
            if (status == 2 && tarefa.IdStatus != 3)
            {
                tarefa.Inicio = DateTime.Now;
            }
            if (status == 4)
            {
                tarefa.Fim = DateTime.Now;
            }
            daotarefa.Salvar(tarefa);
            Session["sucesso"] = "Status Aleterado com sucesso";
            return "ok";

        }
        //
        // GET: /Tarefa/Edit/5

        public ActionResult Editar(int id)
        {
            return View(new DaoTarefa().ObterPorID(id));
        }

        //
        // POST: /Tarefa/Edit/5

        [HttpPost]
        public ActionResult Editar(Tarefa tarefa, FormCollection collection)
        {
            try
            {
                DaoTarefa dao = new DaoTarefa();
                // TODO: Add update logic here
                Tarefa anttarefa = dao.ObterPorID(tarefa.Id);
                anttarefa.Descricao = tarefa.Descricao;
                anttarefa.Inicio = tarefa.Inicio;
                anttarefa.Fim = tarefa.Fim;
                anttarefa.Prioridade = tarefa.Prioridade;
                Session["sucesso"] = "Salvo com sucesso";
                return Redirect("~/tarefa/index/" + anttarefa.IdSprint);
            }
            catch
            {
                return View();
            }
        }

   
        public ActionResult Excluir(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                DaoTarefa dao = new DaoTarefa();
                Tarefa sprint = dao.ObterPorID(id);
                int idsprint = sprint.Id;
                    dao.Excluir(sprint);
                    Session["Sucess"] = "Sprint Excluida com sucesso";

                    return Redirect("~/tarefa/index/" + idsprint);
            }
            catch
            {
                return View();
            }
        }
    }
}
